﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;

namespace TRIG_MOD_STATUSSUMMARY.TCE_StatusSummaries
{
    public partial class TCE_StatusSummariesUserControl : UserControl
    {
        DataForStatusSummary objCls = new DataForStatusSummary();

        SPSite oSite;
        SPWeb oWeb;

        List<string> docTypes = new List<string>();
        List<string> discipAbb = new List<string>();

        public List<string> DocTypes
        {
            get { return docTypes; }
            set { docTypes = value; }
        }

        public List<string> Disciplines
        {
            get { return discipAbb; }
            set { discipAbb = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DocTypes = objCls.GetDocumentType(ddlDocsTypes);
                ddlDocsTypes.Items.Add("Inspection Requests");
                Disciplines = objCls.GetDiscipiline();
            }
            catch (Exception x)
            {
                errorMessage.Text += x.Message;
            }

            DateValidation();
        }

        private void DateValidation()
        {
            stDT.MaxDate = DateTime.Today;
            endDT.MaxDate = DateTime.Today;
            stSumGrid.DataSource = null;
            stSumGrid.Visible = false;
            if (Convert.ToInt32(rdDates.SelectedItem.Value) == 0)
            {
                endDT.Visible = false;
                stDT.IsRequiredField = true;
                stDT.SelectedDate = DateTime.Today;
            }
            else
            {
                endDT.Visible = true;
                stDT.IsRequiredField = true;
                endDT.IsRequiredField = true;
            }
        }

        protected void btnProcessTCE_Click(object sender, EventArgs e)
        {

            string doc = ddlDocsTypes.SelectedItem.Text;
            

            if (txtTest.Text == "DR")
            {
                Process_DR();
            }
            else if (txtTest.Text == "IR")
            {
                Process_IR();
            }
            else if (txtTest.Text == "IRS")
            {
                Process_IRS();
            }
            else if (txtTest.Text == "IRS")
            {
                Process_IRS();
            }
            else if (txtTest.Text == "DS")
            {
                Process_DS();
            }
            else if (txtTest.Text == "DOC")
            {
                Process_DOC();
            }

        }

        private void Process_DR()
        {
            NullLabels_DR();
            discipAbb = objCls.GetDiscipiline();
            objCls.CreateTable("DR");
            if (Convert.ToInt32(rdDates.SelectedItem.Value) == 1)
                GetDates_DR();
            else
                GetDates_DR("Today");
            objCls.rdDates = rdDates;
            objCls.grid = stSumGrid;

            objCls.GetDocItemsbyDiscipiline(ddlDocsTypes.SelectedItem.Text, Disciplines, errorMessage, "DR");
            objCls.BindGrid(stSumGrid, "DR");
        }

        private void Process_IR()
        {
            NullLabels();
            SetGetDates();
            string Type = "CNT";
            if (Type == "CNT")
            { objCls.CreateTable("IR_CNT"); objCls.Group = ("CNT"); }
            else { objCls.CreateTable("IR_CNS"); objCls.Group = ("CNS"); }

            int ck = objCls.GetDocItemsbyDiscipiline_MM(ddlDocsTypes.SelectedItem.Text, Disciplines, errorMessage, "MM");
            if (ck > 0)
                objCls.BindGrid(stSumGrid, "MM");
            else { }
        }

        private void Process_IRS()
        {

            SetGetDates();
            string Type = "CNT";
            if (Type == "CNT")
            { objCls.CreateTable("IR_CNT"); objCls.Group = ("CNT"); }
            else { objCls.CreateTable("IR_CNS"); objCls.Group = ("CNS"); }

            int ckData = objCls.GetDocItemsbyDiscipiline("Inspection Requests", errorMessage, "IR");
            if (ckData > 0)
            {
                objCls.BindGrid(stSumGrid, "IR");
                stSumGrid.Visible = true;
            }
            else { }
        }

        private void Process_DS()
        {
            NullLabels();
            SetGetDates();
            objCls.CreateTable("DS");
            objCls.Group = ("CNT");
            int ck = objCls.GetDocItemsbyDiscipiline_DS(ddlDocsTypes.SelectedItem.Text, Disciplines, errorMessage, "DS");
            if (ck > 0)
            {
                objCls.BindGrid(stSumGrid, lbl_R1, lbl_R2, lbl_R3, lbl_R4, lbl_R5, "DS");
                string p = " % of MSs ";
                lblr1.Text = "Under Review" + p;
                lblr2.Text = "Approved (Code : 1) as per total submitted" + p;
                lblr3.Text = "Approved as noted (Code : 2) as per total submitted" + p;
                lblr4.Text = "Not Approved (Code : 3) as per total submitted" + p;
                lblr5.Text = "Superseded as per total submitted" + p;
            }
            else { }

        }

        private void Process_DOC()
        {
            NullLabels();

            SetGetDates();
            //objCls.GridCSS(stSumGrid);
            objCls.CreateTable("contractor");
            objCls.Group = ("CNT");
            int ck = objCls.GetDocItemsbyDiscipiline_DSS(ddlDocsTypes.SelectedItem.Text, Disciplines, errorMessage, "DSS");
            if (ck > 0)
            {
                objCls.BindGrid(stSumGrid, lbl_R1, lbl_R2, lbl_R3, lbl_R4, lbl_R5, "DSS");
                if (stSumGrid.Rows.Count > 1)
                {
                    string p = "% of MSs ";
                    lblr1.Text = "Under Review" + p;
                    lblr2.Text = "Approved (Code : 1) as per total submitted" + p;
                    lblr3.Text = "Approved as noted (Code : 2) as per total submitted" + p;
                    lblr4.Text = "Not Approved (Code : 3) as per total submitted" + p;
                    lblr5.Text = "Superseded as per total submitted" + p;
                }
                else { lblr1.Text = ""; lblr2.Text = ""; lblr3.Text = ""; lblr4.Text = ""; lblr5.Text = ""; lbl_R1.Text = ""; lbl_R2.Text = ""; lbl_R3.Text = ""; lbl_R4.Text = ""; lbl_R5.Text = ""; }
            }
            else { }
        }

        private void SetGetDates()
        {
            objCls.rdDates = rdDates;
            if (Convert.ToInt32(rdDates.SelectedItem.Value) == 1) // As of Today
                GetDates();
            else
                GetDates("Today");
            objCls.grid = stSumGrid;
        }

        private void GetDates(string today)
        {
            string startDt = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            objCls.cpStDT = startDt;
            objCls.cpEndDT = startDt;

        }

        private void GetDates()
        {
            //stDT.IsRequiredField = true;
            //endDT.IsRequiredField = true;
            string startDt = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            string endDt = Convert.ToString(endDT.SelectedDate.ToShortDateString());
            objCls.cpStDT = startDt;
            objCls.cpEndDT = endDt;

        }

        private void GetDates_DR()
        {
            string thisMonthStart = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            DateTime tmStDt = Convert.ToDateTime(thisMonthStart);
            thisMonthStart = Convert.ToString(tmStDt.AddDays(1 - tmStDt.Day));
            string thisMonthToday = Convert.ToString(new DateTime(tmStDt.Year, tmStDt.Month, 1).AddMonths(1).AddDays(-1)); //DateTime.Now.ToLongTimeString();
            //var yr = DateTime.Today.Year;
            //var mth = DateTime.Today.Month;
            var yr = stDT.SelectedDate.Year;
            var mth = stDT.SelectedDate.Month;
            var firstDay = new DateTime(yr, mth, 1).AddMonths(-1);
            var lastDay = new DateTime(yr, mth, 1).AddDays(-1);
            string lastMonthStart = Convert.ToString(firstDay);
            string lastMonthEnd = Convert.ToString(lastDay);

            objCls.cpStDT = thisMonthStart;
            objCls.cpEndDT = thisMonthToday;
            objCls.lpStDt = lastMonthStart;
            objCls.lpEndDt = lastMonthEnd;
        }

        private void GetDates_DR(string today)
        {
            string todays = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            string yesterday = Convert.ToString(stDT.SelectedDate.AddDays(-1));
            objCls.cpStDT = todays;
            objCls.cpEndDT = todays;
            objCls.lpStDt = yesterday;
            objCls.lpEndDt = yesterday;
        }

        private void NullLabels_DR()
        {
            errorMessage.Text = "";
            stSumGrid.Visible = true;
        }

        private void NullLabels()
        {
            errorMessage.Text = "";
            stSumGrid.Visible = true;
        }

        protected void rdDates_SelectedIndexChanged(object sender, EventArgs e)
        {
            stDT.ClearSelection();
            endDT.ClearSelection();
            stSumGrid.DataSource = null;
            stSumGrid.Visible = false;

            if ("dd" == "ttr")
            {
                lblr1.Text = ""; lblr2.Text = ""; lblr3.Text = ""; lblr4.Text = ""; lblr5.Text = ""; lbl_R1.Text = ""; lbl_R2.Text = ""; lbl_R3.Text = ""; lbl_R4.Text = ""; lbl_R5.Text = "";
            }
            errorMessage.Text = "";
        }

        protected void stSumGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            stSumGrid.PageIndex = e.NewPageIndex;
            stSumGrid.DataBind();
        }

        


    }
}
